package com.htsk.city.services.core.dao;

import com.htsk.city.services.core.domain.TechnicianFavorite;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface TechnicianFavoriteDao extends JpaRepository<TechnicianFavorite, Long> {
    // 检查用户是否已收藏某个技师
    boolean existsByUserIdAndTechnicianId(Long userId, Long technicianId);

    // 根据用户ID和技师ID查找收藏记录
    TechnicianFavorite findByUserIdAndTechnicianId(Long userId, Long technicianId);

    // 获取用户的收藏列表（分页）
    Page<TechnicianFavorite> findByUserId(Long userId, Pageable pageable);

    // 获取用户的收藏列表（不分页）
    List<TechnicianFavorite> findByUserId(Long userId);

    // 获取用户的收藏技师ID列表
    @Query("SELECT tf.technicianId FROM TechnicianFavorite tf WHERE tf.userId = :userId")
    List<Integer> findTechnicianIdsByUserId(@Param("userId") Long userId);

    // 统计用户的收藏数量
    long countByUserId(Long userId);

    // 删除用户的某个收藏
    @Transactional
    @Modifying
    int deleteByUserIdAndTechnicianId(Long userId, Long technicianId);

    // 删除用户的所有收藏
    @Transactional
    @Modifying
    int deleteByUserId(Long userId);

}
